#include "CanvasToolBarModel.h"
#include "Sington.h"
#include <QVariant>
#include <QDebug>

void initTool(QList<Tool>& tools)
{
    Tool cancel;
    cancel.m_name    = "取消";
    cancel.m_type    = ToolType::Cancel;
    cancel.m_message = "";
    cancel.m_norImg  = "../images/arrow_black.png";
    cancel.m_actImg  = "../images/arrow_gray.png";
    tools.append(cancel);

    Tool rect;
    rect.m_name    = "矩形";
    rect.m_type    = ToolType::Rectangle;
    rect.m_message = "矩形";
    rect.m_norImg  = "../images/rectangle_black.png";
    rect.m_actImg  = "../images/rectangle_gray.png";
    tools.append(rect);

    Tool circle;
    circle.m_name    = "圆形";
    circle.m_type    = ToolType::Circle;
    circle.m_message = "圆形";
    circle.m_norImg  = "../images/circle_black.png";
    circle.m_actImg  = "../images/circle_gray.png";
    tools.append(circle);

    Tool backout;
    backout.m_name    = "撤销";
    backout.m_type    = ToolType::Backout;
    backout.m_message = "";
    backout.m_norImg  = "../images/backout_black.png";
    backout.m_actImg  = "../images/backout_gray.png";
    tools.append(backout);

    Tool clear;
    clear.m_name    = "清空";
    clear.m_type    = ToolType::Clear;
    clear.m_message = "";
    clear.m_norImg  = "../images/drawback_black.png";
    clear.m_actImg  = "../images/drawback_gray.png";
    tools.append(clear);

    Tool save;
    save.m_name    = "保存";
    save.m_type    = ToolType::Save;
    save.m_message = "";
    save.m_norImg  = "../images/save_black.png";
    save.m_actImg  = "../images/save_gray.png";
    tools.append(save);

    Tool load;
    load.m_name    = "打开";
    load.m_type    = ToolType::Load;
    load.m_message = "";
    load.m_norImg  = "../images/open_black.png";
    load.m_actImg  = "../images/open_gray.png";
    tools.append(load);
}

CanvasToolBarModel *CanvasToolBarModel::instance()
{
    return Sington<CanvasToolBarModel>::getInstance();
}

CanvasToolBarModel::CanvasToolBarModel(QObject *parent)
    : QAbstractListModel(parent)
{
    initTool(m_tools);
}

int CanvasToolBarModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_tools.size();
}

QVariant CanvasToolBarModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    QVariant var;
    switch(role)
    {
    case RoleName::ToolName:
        var = QVariant::fromValue(m_tools.at(index.row()).m_name);
        break;
    case RoleName::ToolType:
        var = m_tools.at(index.row()).m_type;
        break;
    case RoleName::ToolMessage:
        var = m_tools.at(index.row()).m_message;
        break;
    case RoleName::ToolNorImg:
        var = m_tools.at(index.row()).m_norImg;
        break;
    case RoleName::ToolActImg:
        var = m_tools.at(index.row()).m_actImg;
        break;
    default:
        break;
    }
    return var;
}

QHash<int, QByteArray> CanvasToolBarModel::roleNames() const
{
    QHash<int, QByteArray> roles = {
        {RoleName::ToolType, "toolType"},
        {RoleName::ToolName, "toolName"},
        {RoleName::ToolMessage, "toolMessage"},
        {RoleName::ToolNorImg, "toolNorImg"},
        {RoleName::ToolActImg, "toolActImg"}
    };
    return roles;
}
